var EpisodeIDTable = {
    id: "EpisodeIDTable",	//表格id
    selEpisode: null,		//选中的就诊记录
    table: null,
    tabIndex: 0,          //当前打开的页签索引
    initTabFunc: [],       //各页签内容解析方法
    clearTabFunc: []        //各页签内容清除方法
};

/**
 * 初始化表格的列
 */
EpisodeIDTable.initColumn = function () {
    return [
        { field: 'selectItem', radio: true },
        { title: 'EpisodeID', field: 'id', align: 'center', valign: 'middle', sortable: true },
        { title: '姓名', field: 'name', align: 'center', valign: 'middle', sortable: true, width: 80 }
    ];
};

EpisodeIDTable.clear = function () {
    $("#PatientID").searchbox('setValue', '');
    $("#EpisodeID").searchbox('setValue', '');
    $("#PatName").searchbox('setValue', '');
    EpisodeIDTable.selEpisode = null;
    EpisodeIDTable.table.clear();
    EpisodeIDTable.table.refresh({ query: { NotQuery: true } });
    EpisodeIDTable.clearTabFunc[EpisodeIDTable.tabIndex - 1]();
}
EpisodeIDTable.enter = function (id, value) {
    var organizationId = $("#organizationId").combobox("getValues")[0];
    if (organizationId == "") organizationId = 0;
    var queryData = "";
    value = value.trim().replace(/\s/g, "");
    if (value == "") {
        queryData = '{"NotQuery":"true"}';
    } else {
        if (id == "PatientID") queryData = '{"NotQuery":"false","patientCode":"' + value + '","organizationId":"' + organizationId + '"}';
        if (id == "EpisodeID") queryData = '{"NotQuery":"false","EpisodeID":"' + value + '","organizationId":"' + organizationId + '"}';
        if (id == "PatName") queryData = '{"NotQuery":"false","name":"' + value + '","organizationId":"' + organizationId + '"}';
    }

    queryData = JSON.parse(queryData);
    EpisodeIDTable.table.clear();
    EpisodeIDTable.table.refresh({ query: queryData });
    $("#" + id).searchbox('setValue', '');
}
EpisodeIDTable.ClickRow = function (r, e, f) {
    EpisodeIDTable.selEpisode = r;
    EpisodeIDTable.initTabFunc[EpisodeIDTable.tabIndex - 1]();
}

/********************
  定义各个页签的功能
 ********************/
/**
 * 基本信息
 */
EpisodeIDTable.initPatInfo = function () {
    if (EpisodeIDTable.selEpisode) {
        var patientCode = EpisodeIDTable.selEpisode.patientCode;
        var pat = new $ax(Sys.backendPath + "/cdc/cdcpatient/getByCode/" + patientCode, function (data) {
            console.log(data);
            EpisodeIDTable.patInfo = data;
            setPatInfo(data);  //定义于PatInfo.tag
        }, function () {
            Sys.info("没有数据")
            clearPatInfo();  //定义于PatInfo.tag
        });
        pat.start();
    }
}
EpisodeIDTable.clearPatInfo = function () {
    clearPatInfo();  //定义于PatInfo.tag
}
/**
 * 就诊记录
 */
EpisodeIDTable.initAdmInfo = function () {
    if (typeof CdcEpisode == "undefined") {
        $.getScript("/static/backend/cdc/cdcepisode/cdcepisode.js");
    } else {
        CdcEpisode.query();
    }
}
EpisodeIDTable.clearAdmInfo = function () {
    CdcEpisode.table.clear();
    CdcEpisode.table.refresh({ query: { NotQuery: true } });
}
/**
 * 诊断
 */
EpisodeIDTable.initDiag = function () {
    if (typeof CdcDiagnosis == "undefined") {
        $.getScript("/static/backend/cdc/cdcdiagnosis/cdcdiagnosis.js");
    } else {
        CdcDiagnosis.query();
    }
}
EpisodeIDTable.clearDiag = function () {
    CdcDiagnosis.table.clear();
    CdcDiagnosis.table.refresh({ query: { NotQuery: true } });
}
/**
 * 手术
 */
 EpisodeIDTable.initSurg = function () {
    if (typeof CdcSurgery == "undefined") {
        $.getScript("/static/backend/cdc/cdcsurgery/cdcsurgery.js");
    } else {
        CdcSurgery.query();
    }
}
EpisodeIDTable.clearSurg = function () {
    CdcSurgery.table.clear();
    CdcSurgery.table.refresh({ query: { NotQuery: true } });
}
/**
 * 医嘱
 */
 EpisodeIDTable.initOrdExec = function () {
    if (typeof CdcOrdexec == "undefined") {
        $.getScript("/static/backend/cdc/cdcordexec/cdcordexec.js");
    } else {
        CdcOrdexec.query();
    }
}
EpisodeIDTable.clearOrdExec = function () {
    CdcOrdexec.table.clear();
    CdcOrdexec.table.refresh({ query: { NotQuery: true } });
}
/**
 * 检验
 */
 EpisodeIDTable.initLabReport = function () {
    var tab = $('#busiDataTabs').tabs('getSelected');  // 获取选择的面板
    tab.panel('refresh', '/cdc/labreport');
}
EpisodeIDTable.clearLabReport = function () {
    LabReport.table.clear();
    LabReport.table.refresh({ query: { NotQuery: true } });
}
/**
 * 检查
 * by xzl
 */
EpisodeIDTable.initExamination=function () {
    if (typeof CdcExamination == "undefined") {
        $.getScript("/static/backend/cdc/cdcexamination/cdcexamination.js");
    } else {
        CdcExamination.query();
    }

}
/**
 * 检查
 * by xzl
 */
EpisodeIDTable.clearExamination = function () {
    CdcExamination.table.clear();
    CdcExamination.table.refresh({ query: { NotQuery: true } });
}
/**
 * 病历
 * by xzl
 */
EpisodeIDTable.initEmr = function (){
    var tab = $('#busiDataTabs').tabs('getSelected');  // 获取选择的面板
    tab.panel('refresh', '/cdc/cdcemr');
}
/**
 * 费用
 * by xzl
 */

EpisodeIDTable.initExpense=function (){
    // if(typeof CdcExpense=="undefined"){
    //     $.getScript("/static/backend/cdc/cdcexpense/cdcexpense.js")
    // }else{
    //     CdcExpense.query();
    // }
    var tab = $('#busiDataTabs').tabs('getSelected');  // 获取选择的面板
    tab.panel('refresh', '/cdc/cdcexpense');
}

EpisodeIDTable.clearExpense = function () {
    CdcExpense.table.clear();
    CdcExpense.table.refresh({ query: { NotQuery: true } });
}

/**
 * 按照html中的顺序push各方法
 * 未做的页签 用null占位?
 */
EpisodeIDTable.initTabFunc.push(EpisodeIDTable.initPatInfo);
EpisodeIDTable.initTabFunc.push(EpisodeIDTable.initAdmInfo);
EpisodeIDTable.initTabFunc.push(EpisodeIDTable.initDiag);
EpisodeIDTable.initTabFunc.push(EpisodeIDTable.initSurg);
EpisodeIDTable.initTabFunc.push(EpisodeIDTable.initOrdExec);
EpisodeIDTable.initTabFunc.push(EpisodeIDTable.initLabReport);
EpisodeIDTable.initTabFunc.push(EpisodeIDTable.initExamination);

EpisodeIDTable.initTabFunc.push(EpisodeIDTable.initEmr);
EpisodeIDTable.initTabFunc.push(null);
EpisodeIDTable.initTabFunc.push(null);
EpisodeIDTable.initTabFunc.push(null);


EpisodeIDTable.initTabFunc.push(EpisodeIDTable.initExpense);


EpisodeIDTable.clearTabFunc.push(EpisodeIDTable.clearPatInfo);
EpisodeIDTable.clearTabFunc.push(EpisodeIDTable.clearAdmInfo);
EpisodeIDTable.clearTabFunc.push(EpisodeIDTable.clearDiag);
EpisodeIDTable.clearTabFunc.push(EpisodeIDTable.clearSurg);
EpisodeIDTable.clearTabFunc.push(EpisodeIDTable.clearOrdExec);
EpisodeIDTable.clearTabFunc.push(EpisodeIDTable.clearLabReport);
EpisodeIDTable.clearTabFunc.push(EpisodeIDTable.clearExamination);

EpisodeIDTable.clearTabFunc.push(null);
EpisodeIDTable.clearTabFunc.push(null);
EpisodeIDTable.clearTabFunc.push(null);
EpisodeIDTable.clearTabFunc.push(null);
EpisodeIDTable.clearTabFunc.push(EpisodeIDTable.clearExpense);
/**
 * 页签切换方法
 */
$HUI.tabs("#busiDataTabs", {
    onSelect: function (title, index) {
        EpisodeIDTable.tabIndex = index;
        EpisodeIDTable.initTabFunc[EpisodeIDTable.tabIndex - 1]();
    }
});

$(function () {
    var defaultColunms = EpisodeIDTable.initColumn();
    var table = new BSTable(EpisodeIDTable.id, Sys.backendPath + "/cdc/cdcepisode/query", defaultColunms);
    table.setPaginationType("client");
    table.setPageSize(20);
    table.setShowRefresh(false);
    table.setShowColumns(false);
    table.setShowToggle(false);
    table.setShowExport(false);
    table.setQueryParams({ NotQuery: true });
    table.setOnClickRow(EpisodeIDTable.ClickRow);
    EpisodeIDTable.table = table.init();
});


